/**
 * Copyright (C), 2015-2019, bogewang有限公司
 * FileName: MemberRegisterServiceImpl
 * Author:   bogewang
 * Date:     2019/9/7 17:38
 * Description: 会员注册接口实现类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bogewang.member.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bogewang.core.base.BaseApiService;
import com.bogewang.core.base.BaseResponse;
import com.bogewang.core.constants.Constants;
import com.bogewang.core.core.utils.MD5Util;
import com.bogewang.member.entity.UserDTO;
import com.bogewang.member.feign.VerificaCodeServiceFeign;
import com.bogewang.member.mapper.UserMapper;
import com.bogewang.member.service.MemberRegisterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 〈会员注册接口实现类〉
 *
 * @author bogewang
 * @create 2019/9/7
 * @since 1.0.0
 */
@RestController
public class MemberRegisterServiceImpl extends BaseApiService<JSONObject> implements MemberRegisterService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VerificaCodeServiceFeign verificaCodeServiceFeign;

    @Override
    @Transactional
    public BaseResponse<JSONObject> register(@RequestBody UserDTO userDTO) {
        // 1.验证参数;
        String userName = userDTO.getUserName();
        //if (StringUtils.isEmpty(userName)) {
        //    return setResultError("用户名不能为空!");
        //}

        String password = userDTO.getPassword();
        if (StringUtils.isEmpty(password)) {
            return setResultError("密码不能为空");
        }

        String mobile = userDTO.getMobile();
        if (StringUtils.isEmpty(mobile)) {
            return setResultError("手机号码不能为空!");
        }

        String registCode = userDTO.getRegistCode();
        if (StringUtils.isBlank(registCode)) {
            return setResultError("注册码不能为空!");
        }

        // 2.验证注册码的正确性;
        BaseResponse<JSONObject> verificaWeixinCodeResponse = verificaCodeServiceFeign.verificaWeixinCode(mobile, registCode);
        if (!verificaWeixinCodeResponse.getRtnCode().equals(Constants.HTTP_RES_CODE_200)) {
            return setResultError(verificaWeixinCodeResponse.getRtnMsg());
        }

        // 3.对用户密码进行加密
        String newPassword = MD5Util.MD5(password);
        userDTO.setPassword(newPassword);

        return userMapper.register(userDTO) > 0 ? setResultSuccess("注册成功!") : setResultError("注册失败!");
    }
}